# Create Microservices Databases Script
Write-Host "Creating microservices databases..." -ForegroundColor Green

# Database names for microservices
$databases = @(
  "microservices",
  "microservices_user", 
  "microservices_miniprogram",
  "microservices_h5",
  "microservices_config",
  "nacos_config"
)

Write-Host "Connecting to MySQL and creating databases..." -ForegroundColor Yellow

# Try to find MySQL client
$mysqlCmd = Get-Command mysql -ErrorAction SilentlyContinue
if ($mysqlCmd) {
  Write-Host "Found MySQL client, creating databases..." -ForegroundColor Green
    
  foreach ($database in $databases) {
    Write-Host "Creating database: $database" -ForegroundColor Yellow
        
    try {
      mysql -u root -plgd335208 -e "CREATE DATABASE IF NOT EXISTS $database CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
            
      if ($LASTEXITCODE -eq 0) {
        Write-Host "Database '$database' created successfully!" -ForegroundColor Green
      }
      else {
        Write-Host "Failed to create database '$database'" -ForegroundColor Red
      }
    }
    catch {
      Write-Host "Error creating database '$database': $($_.Exception.Message)" -ForegroundColor Red
    }
  }
    
  # Show all databases
  Write-Host "`nAll databases:" -ForegroundColor Cyan
  mysql -u root -plgd335208 -e "SHOW DATABASES;" 2>$null
    
}
else {
  Write-Host "MySQL client not found in PATH!" -ForegroundColor Red
  Write-Host "Please create databases manually in Beekeeper Studio:" -ForegroundColor Yellow
    
  foreach ($database in $databases) {
    Write-Host "CREATE DATABASE IF NOT EXISTS $database CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" -ForegroundColor White
  }
}

Write-Host "`nDatabase creation completed!" -ForegroundColor Green
Write-Host "Connection details for Beekeeper Studio:" -ForegroundColor Cyan
Write-Host "Host: localhost" -ForegroundColor White
Write-Host "Port: 3306" -ForegroundColor White
Write-Host "Username: root" -ForegroundColor White
Write-Host "Password: lgd335208" -ForegroundColor White
